from machine import Pin, SPI,RTC,Timer
from ssd1306 import SSD1306_SPI   # 导入SSD控制的OLED程序
import os
import utime
import network
import time
import urequests

wlan=0

class NETWORK():
    def __init__(self,ID,password):
        self.wlan=network.WLAN(network.STA_IF)
        self.wlan.active(True)
        self.wlan.disconnect()
        self.wlan.connect(ID, password)
        # 1010 为连接成功
        print(self.wlan.status())
def connectWiFi(ID,password):
  global wlan
  wlan=network.WLAN(network.STA_IF)
  wlan.active(True)
  wlan.disconnect()
  wlan.connect(ID, password)
  return True

def get_time():
  URL="http://quan.suning.com/getSysTime.do"
  res=urequests.get(URL).text
  temp=res.split('"')
  print(temp)
  data=temp[7]
  print(data)
  date=[data[:4],data[4:6],data[6:8],data[8:10],data[10:12],data[12:]]
  got_time=[]
  for str in date :
    got_time.append(int(str))
  return got_time
  
def set_time():
    time=get_time()
    rtc=RTC()
    rtc.init((time[0], time[1], time[2], 4, time[3], time[4], time[5], 0))
    return rtc

def display_time(rtc):
    time=rtc.datetime()
    oled.fill(0)
    oled.text(str(time[0])+' '+str(time[1])+' '+str(time[2]),24,0)
    oled.text(str(time[4])+':'+str(time[5])+':'+str(time[6]),40,16)
    oled.show()

# connectWiFi('EHOME_7E9D','18503451399')
if __name__ == "__main__":
    from OLED import *
    oled = OLED_init()
    network = NETWORK('EHOME_7E9D','18503451399')
    oled.fill(0)
    oled.text('connecting WIFI',0,0)
    # 同上面的道理我们显示设备的IP地址,需要前面链接上网络
    oled.text(network.wlan.ifconfig()[0], 0, 32)
    oled.show()